"""
通用依赖：分页、数据库、脱敏等
根级 common.database 已提供连接池，这里只做快捷别名。
"""

from typing import Annotated, Optional

from fastapi import Depends, Query

from app.common.auth import audit_log
from app.schemas.device_schemas import DeviceQuery
from app.service.alarm_service import AlarmStatisticsService
from app.service.dashboard_service import DashboardService, get_dashboard_service
from app.service.telemetry_service import TelemetryService
from common.database import DatabaseService, get_db_dependency
from common.response import PaginationParams


# 定义分页依赖
def pagination_deps(
		page: int = Query(1, ge=1, description="页码"),
		size: int = Query(20, ge=1, le=100, description="每页条数"),
) -> PaginationParams:
	return PaginationParams(page=page, size=size)


# 定义设备查询参数依赖
def device_query_deps(
		page: int = Query(1, ge=1, description="页码"),
		size: int = Query(20, ge=1, le=1000, description="每页条数"),
		keyword: Optional[str] = Query(None, max_length=64, description="关键字(编码/名称/序列号)"),
		status: Optional[str] = Query(None, description="设备状态筛选"),
		manager_code: Optional[str] = Query(None, max_length=64, description="负责人编号"),
		province_code: Optional[str] = Query(None, max_length=6, description="省编码"),
		city_code: Optional[str] = Query(None, max_length=6, description="市编码"),
		area_code: Optional[str] = Query(None, max_length=6, description="区编码"),
) -> DeviceQuery:
	return DeviceQuery(
		page=page,
		size=size,
		keyword=keyword,
		status=status,
		manager_code=manager_code,
		province_code=province_code,
		city_code=city_code,
		area_code=area_code
	)


# 定义实时数据服务依赖
def get_telemetry_service():
	return TelemetryService()


# 定义告警数据服务依赖
def get_statistics_alarm_service():
	return AlarmStatisticsService()


# 可复用依赖： annotated 里只放「函数本身」，不要放「调用结果」
# 分页依赖
PaginationDep = Annotated[PaginationParams, Depends(pagination_deps)]
# 设备查询参数依赖
DeviceQueryDep = Annotated[DeviceQuery, Depends(device_query_deps)]
# 数据库依赖
DbDep = Annotated[DatabaseService, Depends(get_db_dependency)]
# 审计日志依赖
AuditLogDep = Annotated[int, Depends(audit_log())]

# 遥感数据
TelemetryServiceDep = Annotated[TelemetryService, Depends(get_telemetry_service)]
# 数据大屏
DashboardServiceDep = Annotated[DashboardService, Depends(get_dashboard_service)]
# 告警数据
AlarmStatisticsServiceDep = Annotated[AlarmStatisticsService, Depends(get_statistics_alarm_service)]
